#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/10 8:08 下午
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : model.py
# @Software: PyCharm
# @FileDescription：菜单模型累创建

from DatabaseOperation.BaseModel import BaseModel
from sqlalchemy import Column, VARCHAR, Integer, BigInteger, BOOLEAN


class Menus(BaseModel):
    """
    菜单表
    """
    __tablename__ = "sys_menu"
    __table_args__ = ({"comment": "菜单表"})

    pid = Column(BigInteger, nullable=True, default="", comment="上级菜单ID")
    sub_count = Column(Integer, nullable=True, default=0, comment="子菜单数目")
    type = Column(Integer, nullable=True, default="", comment="菜单类型")
    title = Column(VARCHAR(255), nullable=True, default="", comment="菜单标题")
    name = Column(VARCHAR(255), nullable=True, default="", comment="组建名称")
    component = Column(VARCHAR(255), nullable=True, default="", comment="组件")
    sort = Column(Integer, nullable=True, default="", comment="排序")
    icon = Column(VARCHAR(255), nullable=True, default="", comment="图标")
    path = Column(VARCHAR(255), nullable=True, default="", comment="链接地址")
    i_frame = Column(BOOLEAN, nullable=True, default=False, comment="是否外链")
    cache = Column(BOOLEAN, nullable=True, default=False, comment="是否缓存")
    hidden = Column(BOOLEAN, nullable=True, default=False, comment="是否隐藏")
    permission = Column(VARCHAR(255), nullable=True, default="", comment="全新")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {
            "cache": self.cache,
            "hasChildren": True if self.sub_count and self.sub_count > 0 else False,
            "hidden": self.hidden,
            "iFrame": self.i_frame,
            "icon": self.icon,
            "label": self.title,
            "leaf": False if self.sub_count and self.sub_count > 0 else True,
            "menuSort": self.sort,
            "path": self.path,
            "sunCount": self.sub_count,
            "title": self.title,
            "type": self.type,
            "id": self.id,
            "createTime": self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            "updateTime": self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            "createBy": self.create_by,
        }

    # 把SQLAlchemy查询对象转换成字典
    def to_build_title(self):
        return {
            "alwaysShow": True,
            "component": self.component if self.component and self.component != "" else "Layout",
            "hidden": self.hidden,
            "meta": {
                "icon": self.icon,
                "noCache": not self.cache,
                "title": self.title
            },
            "name": self.title,
            "path": self.path,
            "redirect": "noredirect",
            "id": self.id,
            "pid": self.pid
        }

    def to_build_children(self):
        return {
            "component": self.component,
            "hidden": self.hidden,
            "meta": {
                "icon": self.icon,
                "noCache": not self.cache,
                "title": self.title,
            },
            "name": self.name,
            "path": self.path,
            "id": self.id,
            "pid": self.pid
        }

